<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/7
 * Time: 16:50
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Brand extends Common
{
    /*品牌列表首页
     * $list 首页映射的数据
     * */
public function brand(){
    session('urls', null);
    $list=Db('brand')->where('delete_time',null)->paginate();
    $this->assign('list',$list);
    $this->getConAndAct('Goods');
    return $this->fetch('BrandIndex');
}
/*
 * 品牌列表新增
 * $list 查询分类里parent_id为0的类别
 * */
public function BrandAdd(){
    $list=Db('commodity_classify')->where('parent_id = 0')->select();
    $this->assign('list',$list);
     return view('BrandAdd');
    }

    /**
     * 新增功能
     */
    public function add(){
        $name=input('name');
        $parent_cat_id=input('parent_id_1');
        $cat_id=input('parent_id_2');
        $order=input('order');
        $content=input('content');
        $image=input('image');
        $a=Db('commodity_classify')->where('id',$parent_cat_id)->value('name');
        $data=[
            'name'=>$name,
            'order'=>$order,
            'parent_cat_id'=>$parent_cat_id,
            'cat_id'=>$cat_id,
            'descripition'=>$content,
            'cat_name'=> $a,
            'logo'=>$image
        ];
        $res=Db('brand')->insert($data);
        if ($res){
            $return_arr = array(
                'status' => 1,
                'msg'   => '操作成功',
            );
            echo json_encode($return_arr);
        }else{
            $return_arr = array(
                'status' => -1,
                'msg'   => '操作失败',
            );
            echo json_encode($return_arr);
        }
    }
    /**
     * 编辑页面
     */
  public function BrandEdit(){
      $id=input('id');
      $list=Db('commodity_classify')->where('parent_id = 0')->select();
      $cat_list=Db('brand')->where('id',$id)->find();
      $this->assign('list',$list);
      $this->assign('cat_list',$cat_list);
      return view('BrandEdit');

 }
  public function edit(){
      //判断是否上传图片
      if (input('image')!=1){
          //判断图片是否存在
          if (input('img')){
              unlink(input('img'));//删除图片
          }
          $img=input('image');
      }else{
          $img=input('img');
      }
      $name=input('name');
      $id=input('id');
      $parent_cat_id=input('parent_id_1');
      $cat_id=input('parent_id_2');
      $order=input('order');
      $content=input('content');
      $a=Db('commodity_classify')->where('id',$parent_cat_id)->value('name');
      $data=[
          'name'=>$name,
          'order'=>$order,
          'parent_cat_id'=>$parent_cat_id,
          'cat_id'=>$cat_id,
          'descripition'=>$content,
          'cat_name'=> $a,
          'logo'=>$img
      ];
      $res=Db('brand')->where('id',$id)->update($data);
      if ($res){
          $return_arr = array(
              'status' => 1,
              'msg'   => '操作成功',
          );
          echo json_encode($return_arr);
      }else{
          $return_arr = array(
              'status' => -1,
              'msg'   => '操作失败',
          );
          echo json_encode($return_arr);
      }
  }


/*
 品牌列表软删除
*/
public function BrandDelete(){
    $id=input('id');
    $arr=array();
    $res=\app\admin\Model\Brand::destroy($id);//
    if ($res){
        return retJson('',200,'删除成功');
    }else{
        return retJson('',201,'删除失败');
    }
}
    /**
    品牌列表筛选
     */
    public function BrandSearch(){
        $name=input('name');
        $list=Db('brand')->where('delete_time',null)->where('id|name|cat_name|order','like',"%$name%")
            ->paginate(null,false,[
                'query' => request()->param(), ]);
        $this->assign('list',$list);
        return $this->fetch('BrandIndex');
    }

    //图片上传
    public function webuploader(){
        if (session('urls')){
            unlink("./".session('urls'));//删除替换的图片
        }
        $file = $this->request->file('file');//file是传文件的名称
        $info = $file->move(ROOT_PATH .'public/upload/brand');
        $names=str_replace("\\","/",$info->getSaveName());//把反斜杠(\)替换成斜杠(/)
        $temp_path='/upload/brand/';
        session('urls',$temp_path.$names);
        $arr = array(
            'link' =>$temp_path.$names,
            'filename' => $names,
        );
        return json($arr);
    }
}